define(
    function (require) {
        var datasource = require('er/datasource');
        var BaseFormModel = require('common/FormModel');
        var Data = require('./Data');
        var GlobalData = require('common/GlobalData');
        var moment = require('moment');
        var u = require('underscore');

        var TYPES = {
            couponTypes: datasource.constant(
                [
                    {
                        text: "团购 / 套餐",
                        value: '1'
                    },
                    {
                        text: "折扣",
                        value: '2'
                    }/**暂时废弃抵用券,
                    {
                        text: "抵用券",
                        value: '3'
                    }*/
                ]
            )
        };

        function FormModel() {
            BaseFormModel.apply(this, arguments);
            var entityName = this.getUrlType();
            this.addData(new Data(entityName));
            this.putDatasource(TYPES);
        }

        FormModel.prototype.prepare = function () {
            BaseFormModel.prototype.prepare.call(this);
            this.set('uploadUrl', '/api/upload/1');
            this.prepareObject('groupon', 'discount', 'voucher');

            this.prepareTime(
                {
                    timeField: 'startTime1',
                    dateField: 'startDate1'
                },
                {
                    timeField: 'endTime1',
                    dateField: 'endDate1'
                }
            );

            if (this.get('formType') === 'create') {
                // 设置优惠默认类型,团购
                this.set('type', '1');
            }
        }

        FormModel.prototype.fillEntity = function (entity) {
            var entity = BaseFormModel.prototype.fillEntity.apply(this, arguments);
            var startDate1 = entity.startDate1;
            var startTime1 = entity.startTime1;
            if (startDate1 && startTime1) {
                startDate1 = moment(startDate1).format('YYYY-MM-DD');
                entity.startTime1 = moment(startDate1 + ' ' + startTime1, 'YYYY-MM-DD hh:mm A').format('YYYY-MM-DD HH:mm');
            }
            var endDate1 = entity.endDate1;
            var endTime1 = entity.endTime1;
            if (endDate1 && endTime1) {
                endDate1 = moment(endDate1).format('YYYY-MM-DD');
                entity.endTime1 = moment(endDate1 + ' ' + endTime1, 'YYYY-MM-DD hh:mm A').format('YYYY-MM-DD HH:mm');
            }
            entity = u.omit(entity, 'startDate1', 'endDate1');
            return entity;
        };

        FormModel.prototype.validateEntity = function (entity) {
            var errorMsg = [];
            if (errorMsg.length > 0) {
                return errorMsg;
            }
            return BaseFormModel.prototype.validateEntity.apply(this, arguments);
        };

        FormModel.prototype.save = function(entity) {
            return BaseFormModel.prototype.save.apply(this, arguments);
        };

        require('er/util').inherits(FormModel, BaseFormModel);
        return FormModel;
    }
);
